<template>
	<view>
		<!-- 默认当前公司 -->
		<view class="defaultFirm">
			<view>
				<text>当前公司：</text>
				<view class="displaya">
					<view class="beyondText">
						<text>{{ corpName }}</text>
					</view>
				</view>
			</view>
			<view class="default"><text>默认</text></view>
		</view>

		<!-- 切换当前公司，循环出列表 -->
		<view class="content">
			<text>已绑定公司</text>
			<view class="container" v-for="(item, index) in corpUserList" @click="backCompany(item)" :key="index">
				<uni-swipe-action>
					<uni-swipe-action-item>
						<view class="defaultOne">
							<view class="hidenView">
								<text>{{ item.corpName }} <text class="certiFied"
										v-if="item.corpType">（待审核）</text></text>
							</view>
							<uni-icons custom-prefix="custom-icon" type="checkmarkempty" size="30" color="#b5001e"
								v-if="corpName==item.corpName"></uni-icons>
						</view>
						<template v-slot:right>
							<view class="slot-button" @click.stop="deletaCompany(item)"><text>删除</text>
							</view>
						</template>
					</uni-swipe-action-item>
				</uni-swipe-action>
			</view>
		</view>

		<!-- 新增企业 -->
		<view class="newEnterprise" @click="newTerprise">
			<image src="http://106.14.56.171:15016/images/prjdetail/addition.png" mode=""></image>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	export default {
		data() {
			return {
				Id: '',
				androidIos: '',
				corpName: '',
				userId: '',
				index: null,
				companyName: '', //获取公司名称
				corpUserList: [],
				CompanyName: '',
				// 循环列表
				flag: false,
				// 页码
				pageNo: 1,
				// 每页数量
				pageSize: 20,
				// 搜索条件
				searchValue: '',
				// 是否前往天眼查查询
				isOpenOtherSearch: 0,
				// 公司code
				shipyardCode: '',
				// 公司名称
				totalPages: 1,
				// 公司地址
				companyAddress: '',
				ifBottomRefresh: false,
				loadmore: 'more',
				contentText: {
					contentdown: '加载',
					contentrefresh: '加载中...',
					contentnomore: '暂无更多数据。'
				},
				list: []
			};
		},
		//第一次加载
		onShow(e) {
			this.userId = this.userInfo.id
			// console.log(this.userInfo.corpUserList);
			this.corpName = this.userInfo.corpName || '';
			this.companyName = this.userInfo.companyName || '';
			this.corpUserList = this.userInfo.corpUserList.map(item => {
				item.leftStyle = 0
				return item;
			});
		},
		onLoad() {
			this.androidIos = getApp().globalData.androidIos
		},
		computed: {
			...mapState(['userInfo', 'userCompany']),
		},
		methods: {
			...mapMutations(['setUserInfo']), // 重要
			// 点击企业返回到个人信息的公司选项
			backCompany(item) {
				let data = {
					corpId: item.corpId,
					corpName: item.corpName
				};
				if (this.corpName == item.corpName) {
					uni.showToast({
						title: '此公司已切换过',
						icon: 'none'
					});
				} else if (item.corpType == 1) {
					return
				} else {
					this.$httpNew.post('authApi/user/ChangeCorp', data).then(res => {
						console.log('resresres', res);
						uni.navigateBack({
							// 返回上一级
							delta: 1
						});
					});
				}
			},
			// 点击删除新增的公司
			deletaCompany(item) {
				if (this.corpName == item.corpName) {
					uni.showToast({
						title: '不能删除已绑定的公司',
						icon: 'none'
					});

				} else {
					this.$httpNew.post('authApi/CorpUser/UserDeleteCorp?Id=' + item.id).then(res => {
						console.log(res, '点击删除新增的公司');
						if (!res) {
							this.corpUserList = this.corpUserList.filter(i => {
								return i.id !== item.id;
							});
							this.$httpNew.post('authApi/auth/GetUserInfo', {
								appId: this.androidIos
							}).then(res => {
								this.setUserInfo(res);
							});
							uni.showToast({
								title: '删除成功',
								icon: 'none'
							});
						}
					}).catch(err => {
						console.log(err, 156);
					})
				}
			},

			// 点击跳转到新增企业页面
			newTerprise() {
				uni.navigateTo({
					url: './user_newcompany'
				});
			},
			// 点击返回顶部
			toTop() {
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 100
				});
			}
		},
		onPageScroll(e) {
			//根据距离顶部距离是否显示回到顶部按钮
			if (e.scrollTop > 20) {
				//当距离大于10时显示回到顶部按钮
				this.flag = true;
			} else {
				this.flag = false;
			}
		}
	};
</script>

<style lang="less" scoped>
	// 默认当前公司
	.defaultFirm {
		width: 93%;
		height: 91rpx;
		line-height: 91rpx;
		background-color: #ffffff;
		margin: auto;
		border-radius: 10rpx;
		margin-top: 19rpx;
		display: flex;
		justify-content: space-around;
	}

	.defaultOne {
		width: 100%;
		height: 71rpx;
		line-height: 91rpx;
		padding-left: 38rpx;
		display: flex;

		.hidenView {
			width: 70%;
			height: 71rpx;
			line-height: 91rpx;
			overflow: hidden;
			white-space: nowrap;
			text-overflow: ellipsis;
			word-break: break-all;

			.certiFied {
				color: #A8A8A8;
			}
		}

		/deep/ .uni-icons {
			margin-left: 10%;
		}
	}

	.default {
		width: 80rpx;
		height: 53rpx;
		line-height: 53rpx;
		margin-left: 38rpx;
		margin-top: 21rpx;
		background-color: #f3e9d7;
		text-align: center;
		color: #c49338;
		font-size: 24rpx;
		border-radius: 10rpx;
	}

	.defaultFirm>view:nth-child(1) {
		width: 70%;
		display: flex;
		justify-content: space-around;
	}

	.displaya {
		display: flex;
	}

	.beyondText {
		width: 320rpx;
		height: 91rpx;
		/*溢出隐藏,超出部分变成省略号*/
		white-space: nowrap;
		text-overflow: ellipsis;
		overflow: hidden;
		word-break: break-all;
		text-align: center;
		// background-color: aliceblue;
	}

	// 新增企业
	.newEnterprise {
		position: fixed;
		top: 600px;
		left: 580rpx;
		// position: absolute;
		// bottom: 100rpx;
		// left: 580rpx;
		width: 108rpx;
		height: 108rpx;
		border-radius: 50%;
		display: flex;
		flex-direction: row;
		justify-content: space-around;
		align-items: center;
	}

	.newEnterprise>image {
		width: 103rpx;
		height: 103rpx;
	}

	// 绑定公司
	.bindgs {
		width: 93%;
		background-color: #ffffff;
		border-radius: 10rpx;
		margin: auto;
		margin-top: 20rpx;
	}

	.content {
		width: 93%;
		height: 1000px;
		overflow-x: hidden;
		margin: auto;
		margin-top: 20rpx;
		background-color: #ffffff;
		border-radius: 10rpx;
	}

	.content>text {
		position: relative;
		left: 40rpx;
		top: 20rpx;
	}

	.container {
		position: relative;
		top: 26rpx;
		border-radius: 8rpx;
		// background: #f5f7ff;
	}

	.delete-icon {
		position: absolute;
		top: 50%;
		-webkit-transform: translateY(-50%);
		transform: translateY(-50%);
		right: -197rpx;
		width: 130rpx;
		height: 62rpx;
		line-height: 62rpx;
		border-radius: 31rpx;
		font-weight: 500;
		font-size: 32rpx;
		text-align: center;
		color: #ffffff;
		background: #B5001E;
	}

	// company.vue页面的css
	.searchCompany {
		margin-top: 50rpx;
		height: 80rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.slot-button {
		/* #ifndef APP-NVUE */
		display: flex;
		height: 100%;
		/* #endif */
		flex: 1;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		padding: 0 80rpx;
		background-color: #b5001e;

		text {
			color: #ffffff;
		}
	}
</style>
